<template>
  <el-dialog
    :visible="show"
    @update:visible="updateVisiable($event)"
    :title="title"
    width="890px"
    :show-close="true"
    @open="$emit('open')"
    @opened="onOpened"
    @close="cancel"
    @closed="$emit('closed')"
  >
    <el-form
      ref="form"
      :inline="true"
      size="small"
      @keyup.enter.native="search"
    >
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="资源名称">
            <el-input
              placeholder="资源名称"
              v-model="searchModel.name"
              style="width: 180px"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资源类型">
            <el-select v-model="searchModel.type" placeholder="请选择" clearable>
              <el-option
                v-for="item in ResourceType"
                :key="item.value"
                :label="item.name"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="search"
          >查询
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="table"
      :data="tableData.records"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column prop="name" label="资源名称"></el-table-column>
      <el-table-column prop="type" label="资源类型" :formatter="resourceTypeFormatter"></el-table-column>
      <el-table-column align="center" label="选择" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-check"
            @click="choice(scope.row)"
          >选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.total > 0"
      :total="tableData.total"
      :page.sync="tableData.current"
      :limit.sync="tableData.size"
      @pagination="search"
    />
  </el-dialog>
</template>
<script>
  import Pagination from "@/components/Pagination";
  import {ResourceType} from "@/utils/constant/resourceType";
  import {parseTime} from "@/utils/common";
  import {ApiError} from "@/utils/errors";
  import {pageResource} from "@/api/coc/resource";

  export default {
    components: {Pagination},
    props: {
      // 是否显示, 支持 .sync
      show: {
        type: Boolean,
        required: true
      },
      // 打开立刻搜索
      searchOnShow: {
        type: Boolean,
        default: true
      },
      // 标题
      title: {
        type: String,
        default: "选择资源"
      }
    },
    data() {
      return {
        ResourceType,
        searchModel: {
          name: null,
          type: null,
        },
        tableData: {
          records: [],
          total: 0,
          current: 1,
          size: 5
        }
      };
    },
    methods: {
      updateVisiable(v) {
        this.$emit("update:show", v);
      },
      onOpened() {
        this.$emit("opened");
        if (this.searchOnShow) {
          this.search();
        }
      },
      search() {
        const params = {
          name: this.searchModel.name,
          type: this.searchModel.type,
          current: this.tableData.current,
          size: this.tableData.size
        };
        pageResource(params).then(res => {
          if (res.status == 200) {
            if (res.data.errors) {
              return this.$message.error(ApiError.msg(res.data.errors));
            }
            const data = res.data.data;
            this.tableData.records = data.records;
            this.tableData.total = data.total;
            this.tableData.current = data.current;
            this.tableData.size = data.size;
          }
        });
      },
      choice(row) {
        this.$emit("select-resource", { data: row });
      },
      cancel(row) {
        this.$emit("select-resource", { data: row });
      },
      resourceTypeFormatter(row) {
        return ResourceType[row.type-1]["name"];
      },
    }
  };
</script>
<style lang="scss" scoped>
  @import "@/styles/full-form";
  @import "@/styles/common-table";
</style>
